package com.shenzhen.horselive.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.flqy.baselibrary.utils.ViewUtils;
import com.shenzhen.horselive.R;


/**
 * @author lidong on 2016/2/15.
 * @ClassName ToolBarHelper
 * @描述：用于处理ToolBar
 */
public class ToolBarHelper {

    /*上下文，创建view的时候需要用到*/
    private Context mContext;

    /*base view*/
    private LinearLayout mContentView;

    /*用户定义的view*/
    private View mUserView;

    /*toolbar*/
    private Toolbar mToolBar;


    /*视图构造器*/
    private LayoutInflater mInflater;
    private TextView mTitle;

    private TextView mMenuText;
    private TextView mNavigationText;
    private boolean menuIconVisible = true;
    private int menuIcon;

    public ToolBarHelper(Context context, int layoutId) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        init();
        initAppBar();
        initContentLayout(layoutId);


    }


    public ToolBarHelper(Context context, View view) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        init();
        initAppBar();
        initContentLayout(view);


    }

    /*初始化整个内容*/
    private void init() {
        /*直接创建一个帧布局，作为视图容器的父容器*/
        mContentView = new LinearLayout(mContext);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mContentView.setOrientation(LinearLayout.VERTICAL);
        mContentView.setLayoutParams(params);
        mContentView.setFitsSystemWindows(true);
    }

    /*初始化用户定义的布局*/
    private void initAppBar() {
        /*通过inflater获取toolbar的布局文件*/
        View toolbar = mInflater.inflate(R.layout.toolbar, mContentView);
        mToolBar = (Toolbar) toolbar.findViewById(R.id.tool_bar);
        mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
        mToolBar.setTitle("");
        mMenuText = (TextView) toolbar.findViewById(R.id.collect_list);
        mNavigationText = (TextView) toolbar.findViewById(R.id.navigationText);
    }

    /*初始化toolbar*/
    private void initContentLayout(int id) {
        mUserView = mInflater.inflate(id, null);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        if (mUserView.getBackground() == null) {
            mUserView.setBackgroundResource(R.color.windowBackgroundLight);
        }
        mContentView.addView(mUserView, params);


    }

    /*初始化toolbar*/
    private void initContentLayout(View view) {
        mUserView = view;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        if (mUserView.getBackground() == null) {
            mUserView.setBackgroundResource(R.color.windowBackgroundLight);
        }
        mContentView.addView(mUserView, params);


    }


    public ViewGroup getContentView() {
        return mContentView;
    }

    public Toolbar getToolBar() {
        return mToolBar;
    }

    public void hideToolBar() {
        mToolBar.setVisibility(View.GONE);
    }

    public void setTitle(CharSequence title) {
        mTitle.setText(title);
    }

    public void setTitle(int titleResId) {
        mTitle.setText(titleResId);
    }

    public void setNavigationOnClickListener(View.OnClickListener navigationOnClickListener) {
        mToolBar.setNavigationOnClickListener(navigationOnClickListener);
    }

    public void hideNavigationIcon() {
        mToolBar.setNavigationIcon(null);
    }

    public void setNavigationIcon(int navigationIcon) {
        mToolBar.setNavigationIcon(navigationIcon);
    }

    public void setNavigationIcon(Drawable navigationIcon) {
        mToolBar.setNavigationIcon(navigationIcon);
    }

    public void setMenuTextVisibility(int menuTextVisibility) {
        this.mMenuText.setVisibility(menuTextVisibility);
    }

    public boolean isMenuIconVisible() {
        return menuIcon != 0 ? menuIconVisible : false;
    }

    public void setMenuIconVisible(boolean menuIconVisible) {
        this.menuIconVisible = menuIconVisible;
    }

    public void setMenuText(int textResId) {
        mMenuText.setText(textResId);
    }

    public void setMenuText(String text) {
        mMenuText.setText(text);
    }

    public void setOnMenuTextClickListener(View.OnClickListener onMenuTextClickListener) {
        ViewUtils.setOnClickListener(mMenuText, onMenuTextClickListener);
    }

    public void setNavigationText(String navigationText) {
        mNavigationText.setText(navigationText);
    }

    public void setNavigationText(int textResId) {
        mNavigationText.setText(textResId);
    }

    public void setNavigationTextClickListener(View.OnClickListener navigationTextClickListener) {
        mNavigationText.setOnClickListener(navigationTextClickListener);
    }

    public void setMenuIcon(int menuIconResId) {
        this.menuIcon = menuIconResId;
    }

    public int getMenuIcon() {
        return menuIcon;
    }

    public boolean hasMenuIcon() {
        return menuIcon != 0;
    }

    public void setNavigationTextVisibility(int navigationTextVisible) {
        mNavigationText.setVisibility(navigationTextVisible);
    }

    public void setStyleWhite() {
        mToolBar.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        mToolBar.setNavigationIcon(R.mipmap.ico_black_back);
        mTitle.setTextColor(mContext.getResources().getColor(R.color.textColorPrimary));
        mMenuText.setTextColor(mContext.getResources().getColor(R.color.textColorPrimary));
    }

    public void setStyleRed() {
        mToolBar.setBackgroundColor(mContext.getResources().getColor(R.color.red_common));
        mToolBar.setNavigationIcon(R.mipmap.ico_back);
        mTitle.setTextColor(mContext.getResources().getColor(R.color.textColorWhite));
        mMenuText.setTextColor(mContext.getResources().getColor(R.color.textColorWhite));
    }

    public void setStyleRedWithoutBack() {
        mToolBar.setBackgroundColor(mContext.getResources().getColor(R.color.red_common));
//        mToolBar.setNavigationIcon(R.mipmap.ico_back);
        mTitle.setTextColor(mContext.getResources().getColor(R.color.textColorWhite));
        mMenuText.setTextColor(mContext.getResources().getColor(R.color.textColorWhite));
    }
}

